package com.qianfeng.teachdemo.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;


import com.lidroid.xutils.view.annotation.ViewInject;
import com.qianfeng.teachdemo.R;
import com.qianfeng.teachdemo.adapter.VolleyPagerAdapter;

import java.util.ArrayList;

import static android.support.v4.view.ViewPager.OnPageChangeListener;

/**
 * 主界面
 */
public class HomeFragment extends Fragment implements OnPageChangeListener,RadioGroup.OnCheckedChangeListener {
    public static final String TAG = "HomeFragment";
    @ViewInject(R.id.home_viewpager)
    private ViewPager viewPager;
    private RadioGroup radioGroup;
    private ArrayList<Fragment> fragments = new ArrayList<Fragment>();
    private VolleyPagerAdapter pagerAdapter;

    public HomeFragment() {

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Log.e(TAG, "onCreateView");
        View view = inflater.inflate(R.layout.fragment_home, container, false);
        initView(view);
        return view;
    }

    private void initView(View view) {
        radioGroup = (RadioGroup) view.findViewById(R.id.home_rgs);
        viewPager = (ViewPager) view.findViewById(R.id.home_viewpager);
        fragments.add(new WeekActivityFragment());
        fragments.add(new NearFragment());
        pagerAdapter = new VolleyPagerAdapter(fragments, getChildFragmentManager());
        viewPager.setAdapter(pagerAdapter);
        viewPager.setOnPageChangeListener(this);
        radioGroup.setOnCheckedChangeListener(this);
    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int checkId) {
        switch (checkId){
            case  R.id.home_week:
                viewPager.setCurrentItem(0);
            break;
            case R.id.home_near:
                viewPager.setCurrentItem(1);
                break;
        }
    }

    @Override
    public void onPageScrolled(int i, float v, int i2) {

    }

    @Override
    public void onPageSelected(int i) {
        RadioButton radioButton = (RadioButton)radioGroup.getChildAt(i);
        radioButton.setChecked(true);
    }

    @Override
    public void onPageScrollStateChanged(int i) {

    }

}

